<?php
/*
Plugin Name: BPDEV-SEARCH
Plugin URI: http://bp-dev.org/projects/#search-plugin
Description: Adds search features
Version: 0.1
Author: Nicola Greco
Author URI: http://nicolagreco.com
*/

define( 'BPDEV_SEARCH_NAME', 'BPDEV-SEARCH' );
define( 'BPDEV_SEARCH_VERSION', '0.1' );

require_once( 'bp-core.php' );
require_once( 'bpdev-core.php' );

if ( !get_option( 'bpdev-search-status' ) ) add_option( 'bpdev-search-status', 'off' );

if ( !get_option( 'bpdev-search-buddybar-status' ) ) add_option( 'bpdev-search-buddybar-status', 'on' );

if ( 'on' == get_option( 'bpdev-search-status' ) ) {

	if ( 'on' == get_option( 'bpdev-search-buddybar-status' ) )
		add_action( 'bp_adminbar_menus' , 'bpdev_search_form', 10 );

	add_action( 'bpdev_theme_extra_setup_globals', 'bpdev_search_css' );

}
add_action( 'bpdev_admin_plugins_screen' , 'bpdev_search_admin_menu_screen', 3 );
add_action( 'bpdev_admin_plugins_save' , 'bpdev_search_admin_menu_save', 3 );

function bpdev_search_form() {

	?>
	<li id="bpdev-search">
		<form id="search-form" method="post" action="/search">
			<input type="text" value="Search.." name="search-terms" id="bpdev-terms"  onfocus="if(this.value=='Search..' ) this.value='';" onblur="if(this.value=='' ) this.value='Search..';"/>
			<input type="hidden" value="Search" id="search-submit" name="search-submit"/>
		</form>
	</li>
	<?php

}

function bpdev_search_css() {

	bpdev_theme_register_style(
		'bpdev-search', 
		'BuddyBar Search', 
		array( 'BPDEV_Theme_CSS' , 'bpdev_search' ),
		'on',
		'off'
	);

}

function bpdev_search_admin_menu_save() {

	do_action( 'bpdev_admin_save_component' , 'bpdev-search-status' );
	do_action( 'bpdev_admin_save_component' , 'bpdev-search-buddybar-status' );

}

function bpdev_search_admin_menu_screen() {

	?>
	<h3><?php echo BPDEV_SEARCH_NAME; ?></h3>
	<table class="form-table">
		<tr valign="top">
			<th scope="row">Plugin Version</th>
			<td>
				<?php echo BPDEV_SEARCH_VERSION; ?>
			</td>
		</tr>
		<tr>
			<th scope="row">Plugin Status</th>
			<td>
				<select id="bpdev-search-status" name="bpdev-search-status">
					<option value="off"  <?php if ( "off" == get_option( 'bpdev-search-status' ) || !get_option( 'bpdev-search-status' ) ) echo 'selected=""'; ?>>Off</option>
					<option value="on" <?php if ( "on" ==  get_option( 'bpdev-search-status' ) ) echo 'selected=""'; ?>>On</option>
				</select>
			</td>
		</tr>
		<tr>
			<th scope="row">Search field on BuddyBar</th>
			<td>
				<select id="bpdev-search-buddybar-status" name="bpdev-search-buddybar-status">
					<option value="off"  <?php if ( 'off' == get_option( 'bpdev-search-buddybar-status' ) || !get_option( 'bpdev-search-buddybar-status' ) ) echo 'selected=""'; ?>>Off</option>
					<option value="on" <?php if ( 'on' == get_option( 'bpdev-search-buddybar-status' ) ) echo 'selected=""'; ?>>On</option>
				</select>
			</td>
		</tr>
	</table>
	<?php
}

?>